package com.five.wisdomClass.service.impl;

import com.five.wisdomClass.entity.*;
import com.five.wisdomClass.mapper.StudentMapper;
import com.five.wisdomClass.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements IStudentService {

    @Autowired
    StudentMapper studentMapper;

    // 获取个人所在班级的同班同学
    @Override
    public List<Student> getSpecializedClassMember(Student student) {

        // 通过专业班级查询学生的姓名，学号，电话 获取专业班级名称
        String classname = student.getClassname();
        return studentMapper.getClassMates(classname);
    }

    // 获取个人信息
    @Override
    public Student getSelfInfo(Student student) {

        // 查看个人信息 学号 姓名 院系 专业 班级
        String sNumber = student.getSNumber();
        return studentMapper.getSelfInfo(sNumber);
    }

    // 获取公共课程
    @Override
    public List<Course> getPublicCourses(Student student) {
        if (student == null){
            return null;
        }
        // 获取班级名
        String classname = student.getClassname();
        if (classname == null){
            return null;
        }
        // 获取班级号
        String classId = studentMapper.getClassId(classname);
        if (classId == null){
            return null;
        }
        // 获取课程号
        List<String> courseNumberList = studentMapper.getPublicCourseNumber(classId);
        if (courseNumberList == null ||courseNumberList.isEmpty()){
            return null;
        }
        // 获取课程信息
        List<Course> publicCourseList = new ArrayList<>();
        for (String courseNumber : courseNumberList) {
            publicCourseList.add(studentMapper.getPublicCourse(courseNumber));
        }
        return publicCourseList;
    }

    // 获取选修课程
    @Override
    public List<Course> getTakeCourses(Student student) {
        if (student == null){
            return null;
        }
        // 获取学生学号
        String sNumber = student.getSNumber();
        if (sNumber == null){
            return null;
        }
        // 查询课程号
        List<String> courseNumberList = studentMapper.getTakeCourseNumbers(sNumber);
        if (courseNumberList == null || courseNumberList.isEmpty()){
            return null;
        }
        // 查询课程信息
        List<Course> takeCourseList = new ArrayList<>();
        for (String courseNumber : courseNumberList) {
            takeCourseList.add(studentMapper.getTakeCourse(courseNumber));
        }
        return takeCourseList;
    }

    // 获取个人课程分数
    @Override
    public List<Scores> getSelfScores(Student student) {
        if (student == null){
            return null;
        }
        String sNumber = student.getSNumber();
        // 通过学号查询课程成绩
        List<Scores> scoresList = studentMapper.getScores(sNumber);
        return scoresList;
    }

    // 添加个人计划
    @Override
    public boolean addPlan(SelfPlan selfPlan) {
        if (selfPlan.getSNumber() == null){
            return false;
        }
        return studentMapper.addPlan(selfPlan) > 0;
    }

    // 删除个人计划
    @Override
    public boolean deletePlan(Integer id) {
        return studentMapper.deletePlan(id) > 0;
    }

    // 修改个人计划
    @Override
    public boolean editPlan(String context, Integer id) {
        return studentMapper.editPlan(context,id) > 0;
    }

    // 查看个人计划
    @Override
    public List<SelfPlan> getPlans(Student student) {
        if (student == null){
            return null;
        }
        // 获取学号
        String sNumber = student.getSNumber();
        List<SelfPlan> selfPlans=studentMapper.showPlan(sNumber);
        return selfPlans;
    }

    // 学生请假
    @Override
    public boolean submitLeave(Leave leave) {
        // 条件判断
        if (leave.getLeaveType() == null){
            return false;
        }
        if (leave.getStartTime() == null || leave.getEndTime() == null){
            return false;
        }
        if (leave.getContact() == null || leave.getContext() == null){
            return false;
        }
        return studentMapper.submitLeave(leave) > 0;
    }


}
